package com.managerProvider.controller;

import com.alibaba.fastjson.JSON;
import com.managerProvider.service.UserService;
import entity.ResponseMessage;
import entity.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * The type User controller.
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    private Logger logger = Logger.getLogger(this.getClass());

    /**
     * Find all user string.
     *
     * @param user the user
     * @return the string
     */
    @RequestMapping("/findAllUser")
    public String findAllUser(@RequestBody User user) {
        logger.info("UserController findAllUser params:" + user);
        ResponseMessage allUser = userService.findAllUser(user);
        logger.debug("UserController findAllUser params:" + user + ",result:" + allUser);
        return JSON.toJSONString(allUser);
    }

    /**
     * Insert user string.
     *
     * @param user the user
     * @return the string
     */
    @RequestMapping("/insertUser")
    public String insertUser(@RequestBody User user) {
        logger.info("UserController insertUser params:" + user);
        ResponseMessage msg = userService.insertUser(user);
        logger.debug("UserController insertUser params:" + user + ",result:" + msg);
        return JSON.toJSONString(msg);
    }

    /**
     * Update user string.
     *
     * @param user the user
     * @return the string
     */
    @RequestMapping("/updateUser")
    public String updateUser(@RequestBody User user) {
        logger.info("UserController updateUser params:" + user);
        ResponseMessage msg = userService.updateUser(user);
        logger.debug("UserController updateUser params:" + user + ",result:" + msg);
        return JSON.toJSONString(msg);
    }

    /**
     * Delete by id string.
     *
     * @param id the id
     * @return the string
     */
    @RequestMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") String id) {
        logger.info("UserController deleteById params:" + id);
        ResponseMessage msg = userService.deleteById(id);
        logger.debug("UserController deleteById params:" + id + ",result:" + msg);
        return JSON.toJSONString(msg);
    }

    /**
     * Select one user string.
     *
     * @param user the user
     * @return the string
     */
    @RequestMapping("/selectOneUser")
    public String selectOneUser(@RequestBody User user) {
        logger.info("UserController selectOneUser params:" + user);
        ResponseMessage allUser = userService.selectOneUser(user);
        logger.debug("UserController selectOneUser params:" + user + ",result:" + allUser);
        return JSON.toJSONString(allUser);
    }


    @RequestMapping("/selectOneUserRolePower")
    public String selectOneUserRolePower(@RequestBody User user) {
        logger.info("UserController selectOneUserRolePower params:" + user);
        ResponseMessage allUser = userService.selectOneUserRolePower(user);
        logger.debug("UserController selectOneUserRolePower params:" + user + ",result:" + allUser);
        return JSON.toJSONString(allUser);
    }
}
